home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 038a / bas_int1.zip / DIR$.BAS < prev    next >
BASIC Source File  |  1991-06-12  |  3KB  |  85 lines

  1. '=========================================================================
  2. 'DIR.BAS by Dave Cleary
  3. '
  4. 'One of the most useful additions to BASIC 7 PDS is the DIR$ function.
  5. 'This function allows you to read a directory of filenames. It also
  6. 'allows you to check the existence of a file by doing the following:
  7. '
  8. '  IF LEN(DIR$("COMMAND.COM")) THEN
  9. '     PRINT "File Found"
  10. '  ELSE
  11. '     PRINT "File not found"
  12. '  END IF
  13. '
  14. 'Now QuickBASIC 4.X users can have this useful function for their
  15. 'programs.
  16. '
  17. 'Calling DIR$ with a FileSpec$ returns the the name of the FIRST
  18. 'matching file name. Subsequent calls with a null FileSpec$ return the
  19. 'NEXT matching file name. If a null string is returned, then no more
  20. 'matching files were found. FileSpec$ can contain both a drive and a
  21. 'path plus DOS wildcards. Special care should be taken when using
  22. 'this on floppy drives because there is no check to see if the drive
  23. 'is ready.
  24. '========================================================================
  25. 'Main()
  26.      DEFINT A-Z
  27.      
  28.      DECLARE FUNCTION DIR$ (FileSpec$)
  29.      
  30.      '$INCLUDE: 'QB.BI'
  31.      
  32.      '-----  Some constants that DIR$ uses
  33.      CONST DOS = &H21
  34.      CONST SetDTA = &H1A00, FindFirst = &H4E00, FindNext = &H4F00
  35.      
  36.      '--------------------------------------------------------------------
  37.      'This shows how to call DIR$ to find all matching files
  38.      
  39.      'CLS
  40.      'FileSpec$ = "C:\QB\SOURCE\*.BAS"
  41.      'Found$ = DIR$(FileSpec$)
  42.      'DO WHILE LEN(Found$)
  43.      '   PRINT Found$
  44.      '   Found$ = DIR$("")
  45.      'LOOP
  46.      
  47.      '--------------------------------------------------------------------
  48.      
  49.  FUNCTION DIR$ (FileSpec$) STATIC
  50.      
  51.         DIM DTA AS STRING * 44, Regs AS RegTypeX
  52.         Null$ = CHR$(0)
  53.      
  54.      '-----  Set up our own DTA so we don't destroy COMMAND$
  55.         Regs.AX = SetDTA                    'Set DTA function
  56.         Regs.DX = VARPTR(DTA)               'DS:DX points to our DTA
  57.         Regs.DS = -1                        'Use current value for DS
  58.         InterruptX DOS, Regs, Regs          'Do the interrupt
  59.      
  60.      '-----  Check to see if this is First or Next
  61.         IF LEN(FileSpec$) THEN              'FileSpec$ isn't null, so
  62.                                             'FindFirst
  63.            FileSpecZ$ = FileSpec$ + Null$   'Make FileSpec$ into an ASCIIZ
  64.                                             'string
  65.            Regs.AX = FindFirst              'Perform a FindFirst
  66.            Regs.CX = 0                      'Only look for normal files
  67.            Regs.DX = SADD(FileSpecZ$)       'DS:DX points to ASCIIZ file
  68.            Regs.DS = -1                     'Use current DS
  69.         ELSE                                'We have a null FileSpec$,
  70.            Regs.AX = FindNext               'so FindNext
  71.         END IF
  72.      
  73.         InterruptX DOS, Regs, Regs          'Do the interrupt
  74.      
  75.      '-----  Return file name or null
  76.         IF Regs.Flags AND 1 THEN            'No files found
  77.            DIR$ = ""                        'Return null string
  78.         ELSE
  79.            Null = INSTR(31, DTA, Null$)     'Get the filename found
  80.            DIR$ = MID$(DTA, 31, Null - 30)  'It's an ASCIIZ string starting
  81.         END IF                              'at offset 30 of the DTA
  82.      
  83.  END FUNCTION
  84.      
  85.